import { Toast } from 'vant'
import Vue from 'vue'
import Vuex from 'vuex'
import { apiGetCategoryMenu } from '@/api/category.js'
import { apiGetProduct, apiGetBanner } from '@/api/product.js'
import { apiGetCart, apiDeleteCart, apiUpdateCart, apiAddCart } from '@/api/cart.js'
import { apiLogin, apiGetUser, apiEditUser } from '@/api/user.js'
import { apiGetOrder, apiDeleteOrder } from '@/api/order.js'
import { apiGetAddress } from '@/api/address.js'
import createLogger from 'vuex/dist/logger'

const debug = process.env.NODE_ENV !== 'production'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    userInfo: {},
    bannerList: [],
    productList: [],
    categoryList: [],
    menuList: [],
    carts: [],
    cartsLength: 0,
    orderStatus: 'paying',
    orders: [],
    addressStatus: '',
    address: {},
    orderDetail: {},
    defaultAddress: null,
    addressList: [],
  },
  actions: {
    setData: ({ commit, state }, object) => {
      Object.keys(object).map(item => {
        commit('SET_DATA', { name: item, value: object[item] })
      })
    },
    getProductData: ({ commit, state }, pageNum) => {
      return apiGetProduct(pageNum, '热门').then(res => {
        commit('SET_DATA', { name: 'productList', value: res.data.result })
        return res.data.result
      })
    },
    getOrderList: ({ commit, state }, { type, This }) => {
      apiGetOrder(type).then(res => {
        console.log(res)
        let orderData = res.data.result.map(item => {
          if (item.status == 'payed') {
            item.color = 'success'
          } else if (item.status == 'paying') {
            item.color = 'danger'
          } else if (item.status == 'done') {
            item.color = ''
          }
          return item
        })
        console.log(orderData)
        console.log(666)
        commit('SET_DATA', { name: 'orders', value: orderData })
        orderData.length == 0 && Toast({ message: This.$t('m.order.noOrderState'), duration: 500 })
      })
    },
    getBannerList: ({ commit, state }) => {
      return apiGetBanner().then(res => {
        commit('SET_DATA', { name: 'bannerList', value: res.data.result })
        return res.data.result
      })
    },
    getMenuList: ({ commit, state }) => {
      return apiGetCategoryMenu().then(res => {
        commit('SET_DATA', { name: 'menuList', value: res.data.result })
        return res.data.result
      })
    },
    getAddressList: ({ commit, state }) => {
      return apiGetAddress().then(res => {
        console.log(res)
        console.log(555)
        commit('SET_DATA', { name: 'addressList', value: res.data.result || [] })
        if (res.data.result.length) {
          res.data.result.find(item => item.checked)
            ? commit('SET_DATA', { name: 'defaultAddress', value: res.data.result.find(item => item.checked) })
            : commit('SET_DATA', { name: 'defaultAddress', value: res.data.result[0] })
        }
        return res.data.result
      })
    },
    getCartList: ({ commit, state }) => {
      return apiGetCart().then(res => {
        commit('SET_DATA', { name: 'carts', value: res.data.result || [] })
        return res.data
      })
    },
    deleteCartGoods: ({ commit, state }, { item, index }) => {
      return apiDeleteCart(item._id).then(() => {
        state.carts.splice(index, 1)
        commit('SET_DATA', { name: 'carts', value: state.carts })
      })
    },
    getCategoryList: ({ commit, state }, { pageNum, type }) => {
      return apiGetProduct(pageNum, type).then(res => {
        commit('SET_DATA', { name: 'categoryList', value: res.data.result })
        return res.data.result
      })
    },
    getUserInfo: ({ commit, state }) => {
      return apiGetUser().then(res => {
        if (res.data.code == -1) {
          return res.data
        }
        commit('SET_DATA', { name: 'userInfo', value: res.data.result })
        // commit('SET_DATA', { name: 'login', value: res.data.result.address[0] });
        // commit('SET_DATA', { name: 'carts', value: res.data.result.carts });
        return res.data
      })
    },
    getLogin: ({ commit, state }, { user, pwd, This }) => {
      apiLogin(user, pwd).then(res => {
        if (res.data.code == 0) {
          commit('SET_DATA', { name: 'login', value: true })
          // This.onBack();
        } else {
          Toast({
            message: res.data.msg,
          })
        }
      })
    },

    addCart: ({ commit, state }, { id, This }) => {
      return apiAddCart(id).then(res => {
        res.data.code == 1 &&
        Toast.success({
          message: This.$t('m.message.addSuccess'),
          duration: 1000,
        })
        return res.data
      })
    },
  },
  mutations: {
    SET_DATA: (state, { name, value }) => {
      state[name] = value
    },
    SET_ADDRESS_STATUS: (state, item) => {
      state.addressStatus = item
    },
    SET_ORDER_DETAIL: (state, item) => {
      state.orderDetail = item
    },
    SET_ORDER_STATUS: (state, orderStatus) => {
      state.orderStatus = orderStatus
    },
    REST_USER: state => {
      state.userInfo = {}
      state.addressStatus = ''
      state.address = {}
    },
    SET_RESET: state => {
      state.carts = []
      state.userInfo = {}
      state.cartsLength = 0
      state.orderStatus = 'paying'
      state.orders = []
      state.addressStatus = ''
      state.address = {}
    },
  },
  modules: {},
  strice: debug,
  plugins: debug ? [createLogger()] : [],
})
